<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\mongodb\Database;

/**
 * This is the model class for table "mops_user_coupon".
 *
 * @property int $id
 * @property int $user_id 用户
 * @property int $coupon_id 优惠券
 * @property int $catalog 优惠券分类
 * @property int $created_at
 * @property int $updated_at
 * @property int $mark_id
 */
class UserCoupon extends \yii\db\ActiveRecord
{
    const STATUS_ERROR = 1;
    const STATUS_OK = 2;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%user_coupon}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'coupon_id', 'catalog', 'created_at', 'updated_at'], 'integer'],
            [['mark_id'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户',
            'coupon_id' => '优惠券',
            'catalog' => '优惠券分类',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'mark_id'=>'分类标识',
        ];
    }

    public function behaviors()
    {
        return [
            TimestampBehavior::className()
        ];
    }

    public function getErrorMsg()
    {
        $errMsgArr = $this->getErrors();
        $errStr = '';
        if (count($errMsgArr)) {
            foreach ($errMsgArr as $errMsg) {
                $errStr .= $errMsg[0];
            }
        }
        return $errStr;
    }

    public static function getCollection()
    {
        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase();
        return $collection = $database->getCollection("user_coupon");
    }
    public function objToArr()
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'coupon_id' => $this->coupon_id,
            'catalog' => $this->catalog,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'mark_id'=>$this->mark_id,
        ];
    }
    public function afterDelete()
    {
        $collection = self::getCollection();
        $collection->remove(['user_id' => intval($this->user_id),'coupon_id'=>$this->coupon_id]);
        return true;
    }
    public function afterSave($insert, $changedAttributes)
    {
        $collection = self::getCollection();
        $find = $collection->findOne(['user_id' => $this->user_id,'coupon_id'=>$this->coupon_id]);
        if ($find) {
            $collection->update(['user_id' => $this->user_id,'coupon_id'=>$this->coupon_id], $this->objToArr());
        } else {
            $collection->insert($this->objToArr());
        }
        return true;
    }
    public function getCoupon()
    {
        return $this->hasOne(Coupon::className(), ['id' => 'coupon_id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getCouponCategory()
    {
        return $this->hasOne(CouponCategory::className(), ['id' => 'catalog']);
    }
}
